package com.oa.modules.approvalsec1.controller;
import com.oa.common.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
@RestController
@RequestMapping("projectUpload")
@Api(tags = "项目文件上传接口")
public class ProjectUploadController {
    private static String realPath;

    /**
     * ofd证照上传 图片上传
     * @param request
     * @param response
     * @param session
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
    @ResponseBody
    @PostMapping("/uploadFileByUser")
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ApiOperation(value = "文件上传")
    public R uploadFileByUser(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IllegalStateException, IOException {
        String imageHtmlUrl = "";
        //进行分布式对象存储Ambry文件上传
        MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
        request.setCharacterEncoding("utf-8");
        List<MultipartFile> fileList = req.getFiles("file");
        String imageUrlStr = "";
        String imageHtmlUrlStr = "";
        String url = "";
        for (MultipartFile file : fileList){
            if (file != null) {
                String fileName = file.getOriginalFilename();// 文件原名称
                String prefix = fileName.substring(fileName.lastIndexOf(".")+1);
                String imageUrl = FileUtil.ImageUpload(file, request);
                File newFile = new File(imageUrl);
                if (!newFile.isFile()) {
                    return R.error(1,imageUrl);
                }
                //将上传的ofd文件转换成图片
                if (prefix.equals("ofd")) {
                    String pathDir = imageUrl.replace(fileName, "").replace("/file","/image");
                    String jpgFileName = fileName.replace(".ofd","");
                    url = OfdConver.toPng(imageUrl, pathDir, jpgFileName);
                    String[] split = url.split(",");
                    for (int i = 0; i < split.length; i++) {
                        if (i == split.length-1){
                            imageHtmlUrl = split[i].replaceAll("/home/image","https://www.qhspj.com/").replaceAll("/home/file","https://www.qhspj.com/");
                            imageHtmlUrlStr += imageHtmlUrl;
                        }else {
                            imageHtmlUrl = split[i].replaceAll("/home/image", "https://www.qhspj.com/").replaceAll("/home/file", "https://www.qhspj.com/");
                            imageHtmlUrlStr += imageHtmlUrl + ",";
                        }
                    }
                }else{
                    imageHtmlUrl = imageUrl.replaceAll("/home/image", "https://www.qhspj.com/").replaceAll("/home/file", "https://www.qhspj.com/");
                    imageHtmlUrlStr += imageHtmlUrl + ",";
                }
//                imageUrl = imageUrl.replace("ofd","jpg");

//                for (String s : split) {
////                    imageHtmlUrl = s.replaceAll("/home/image","https://www.qhspj.com/").replaceAll("/home/file","https://www.qhspj.com/");
//                    imageHtmlUrl = s.replaceAll("/home/image","https://www.qhspj.com/").replaceAll("/home/file","https://www.qhspj.com/");
//                    System.out.println("图片路径"+imageHtmlUrl);
////                    imageUrlStr += "," + s;
//                    imageHtmlUrlStr += "," + imageHtmlUrl;
//                }
            }
        }
        if(!"".equals(imageUrlStr)){
            imageUrlStr = imageUrlStr.substring(1);
            imageHtmlUrlStr = imageHtmlUrlStr.substring(1);
        }
        imageHtmlUrlStr=imageHtmlUrlStr.substring(0,imageHtmlUrlStr.lastIndexOf(','));

        return R.ok().put("url", imageUrlStr).put("HtmlUrl",imageHtmlUrlStr);
    }

         /* 通用上传文件、图片接口
         * @param request
         * @param response
         * @param session
         * @return
         * @throws IllegalStateException
         * @throws IOException
         *
         */

    /**
     *
     * @param request
     * @param response
     * @param session
     * @return
     * @throws IllegalStateException
     * @throws IOException
     * @throws ParseException
     */
        @ResponseBody
        @PostMapping("/uploadFile")
        @ApiOperation(value = "文件上传")
        public R uploadFileRPA(HttpServletRequest request,HttpServletResponse response, HttpSession session) throws IllegalStateException, IOException, ParseException {

            //进行分布式对象存储Ambry文件上传
            MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
            request.setCharacterEncoding("utf-8");
            List<MultipartFile> fileList = req.getFiles("file");
            String imageUrlStr = "";
            String imageHtmlUrlStr = "";
            for (MultipartFile file : fileList){
                if (file != null) {
                    String fileName = file.getOriginalFilename();// 文件原名称
                    String prefix = fileName.substring(fileName.lastIndexOf(".")+1);
                    if(prefix.equals("gif") || prefix.equals("bmp") || prefix.equals("jpg") || prefix.equals("tif") || prefix.equals("jpeg") || prefix.equals("png") || prefix.equals("PNG")){
                        //保存文件  
                        String imageUrl = FileUtil.ImageUpload(file, request);
                        File newFile = new File(imageUrl);
                        if (!newFile.isFile()) {
                            return R.error(1,imageUrl);
                        }
                        // 公司服务器
//                       imageUrlStr = imageUrl.replaceAll("/home/image","https://www.qhspj.com").replaceAll("/home/file","https://www.qhspj.com");
                        imageUrlStr = imageUrl.replaceAll("/home/image","https://www.qhspj.com").replaceAll("/home/file","https://www.qhspj.com");
                    }

                }
            }
//
            return R.ok().put("HtmlUrl",imageUrlStr);
        }


        /**
         *  删除文件
         */
        @PostMapping("/delFile")
        @ApiOperation(value = "删除文件")
        public R delFile(@RequestBody String path){
            File file = new File(path);
            if(!file.exists()){
                return R.error("文件不存在");
            }
            FileUtil.delAllFile(path);
            return R.ok();
        }




}
